package com.lb.pdm.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lb.pdm.common.constants.RedisConstants;
import com.lb.pdm.common.result.GraceJSONResult;
import com.lb.pdm.entity.SysUser;
import com.lb.pdm.entity.bo.RegisterBO;
import com.lb.pdm.entity.vo.UserVO;
import com.lb.pdm.mapper.SysUserMapper;
import com.lb.pdm.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author pdm
 * @since 2025-10-29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public GraceJSONResult login(String username, String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser queryUser = sysUserMapper.selectOne(queryWrapper);

        if (queryUser == null) {
            return GraceJSONResult.errorMsg("用户不存在，请先注册！");
        }

        if (!passwordEncoder.matches(password, queryUser.getPassword())) {
            return GraceJSONResult.errorMsg("密码错误");
        }

        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(RedisConstants.USER_TOKEN_PREFIX + token, JSON.toJSONString(queryUser), RedisConstants.USER_TOKEN_EXPIRE_TIME, TimeUnit.SECONDS);

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(queryUser, userVO);
        userVO.setToken(token);

        return GraceJSONResult.ok(userVO);
    }

    @Override
    public GraceJSONResult register(RegisterBO registerBO) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", registerBO.getUsername());
        SysUser queryUser = sysUserMapper.selectOne(queryWrapper);

        if (queryUser != null) {
            return GraceJSONResult.errorMsg("用户名已存在");
        }

        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(registerBO, sysUser);

        String originPassword = sysUser.getPassword();
        String encodePassword = passwordEncoder.encode(originPassword);

        sysUser.setPassword(encodePassword);
        sysUser.setStatus((byte) 1);
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());

        sysUserMapper.insert(sysUser);

        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set( RedisConstants.USER_TOKEN_PREFIX + token, JSON.toJSONString(sysUser), RedisConstants.USER_TOKEN_EXPIRE_TIME, TimeUnit.SECONDS);

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(sysUser, userVO);
        userVO.setToken(token);

        return GraceJSONResult.ok(userVO);
    }
}
